/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcpserver;

/**
 *
 * @author makkalot
 * 
 */

import udpserver.LoadImage;
import udpserver.UdpSimpleClient;
import java.io.*;
import java.net.*;


public class SimpleTcpClient {

       
    public static void main (String[] args) throws IOException{
    
        Socket kksocket = null;
        BufferedOutputStream bout = null; // to send to server
        DataOutputStream out = null;
        
        try{
            
            kksocket = new Socket("localhost",SimpleTcpServer.bindPort);//connect to server
            bout = new BufferedOutputStream(kksocket.getOutputStream()); //write bytes to that one
            out = new DataOutputStream(bout);
            
            //create here the stuff to sent
            LoadImage load_video = new LoadImage("send.avi");
            byte[] binary_data = new byte[UdpSimpleClient.MAX_IO_BUFFER];
            boolean stop_me = false;
            
            //in the beginning send start mesage
            String theLine = "start";
            out.write(theLine.getBytes(), 0, theLine.getBytes().length);
            
            System.out.println("I sent the start mesage");
            
            while(!stop_me){
            
                if(load_video.readThatByte(binary_data, UdpSimpleClient.MAX_IO_BUFFER)==-1){
                //if you come here that is the end of the game yoo
                    theLine = "stop";
                    stop_me = true;
                    continue;
                }
                
                //send data here
                out.write(binary_data, 0, binary_data.length);
            
            }
            
            out.write(theLine.getBytes(), 0, theLine.getBytes().length);
            System.out.println("I sent the stoppping mesage");
            
            out.close();
            kksocket.close();
            System.out.println("I closed the connection from server");
        }
       
        catch(UnknownHostException e){
            System.err.println("Error in connection to server 4444");
            System.exit(1);
        }
        catch(IOException e){
            
            System.err.println("Error in IO of connection");
            System.exit(1);
        }

        
    }  
}
